package com.leilei.greendao.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;

import com.leilei.greendao.R;
import com.leilei.greendao.db.GreenDaoManager;
import com.leilei.greendao.util.Util;

import java.util.Date;

public class MainActivity extends AppCompatActivity {
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        linearLayout = (LinearLayout) findViewById(R.id.line1);
    }

    public void add(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final long start_time = System.currentTimeMillis();
                GreenDaoManager.getInstances().insert();
                final long end_time = System.currentTimeMillis();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Util.showSnackBar(linearLayout, "增加成功,耗时：" + (end_time - start_time) + "ms");
                    }
                });
            }
        }).start();

    }

    public void query(View view) {
        startActivity(new Intent(MainActivity.this, RecyclerActivity.class));
    }

    public void update(View view) {
        final long start_time = System.currentTimeMillis();
        String date = new Date().toString();
        GreenDaoManager.getInstances().update(0, date);
        long end_time = System.currentTimeMillis();
        Util.showSnackBar(linearLayout, "无更新数据,耗时：" + (end_time - start_time) + "ms");
    }

    public void delete(View view) {
        final long start_time = System.currentTimeMillis();
        GreenDaoManager.getInstances().delete(0);
        long end_time = System.currentTimeMillis();
        Util.showSnackBar(linearLayout, "删除成功,耗时：" + (end_time - start_time) + "ms");
    }

}
